package com.xgk.boot.module.core.dal.mapper.product;

import com.xgk.boot.framework.common.pojo.PageResult;
import com.xgk.boot.framework.mybatis.core.dataobject.BaseDO;
import com.xgk.boot.framework.mybatis.core.mapper.BaseMapperX;
import com.xgk.boot.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xgk.boot.module.core.controller.admin.product.vo.product.ProductPageReqVO;
import com.xgk.boot.module.core.dal.entity.product.ProductDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface ProductMapper extends BaseMapperX<ProductDO> {

    default ProductDO selectByName(String name) {
        return selectOne(ProductDO::getPrName, name);
    }

    default ProductDO selectByCode(String code) {
        return selectOne(ProductDO::getPrCode, code);
    }
    default PageResult<ProductDO> selectPage(ProductPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ProductDO>()
                .likeIfPresent(ProductDO::getPrName, reqVO.getPrName())
                .likeIfPresent(ProductDO::getPrCode, reqVO.getPrCode())
                .eqIfPresent(ProductDO::getPrStatus, reqVO.getPrStatus())
                .betweenIfPresent(BaseDO::getCreateTime, new Instant[]{reqVO.getStartTime(), reqVO.getEndTime()})
                .orderByAsc(ProductDO::getUpdateTime));
    }

    @Select("SELECT p.p_id,p.p_code,p.p_name FROM product p " +
            "JOIN product_customer pc ON p.p_id = pc.p_id " +
            "JOIN customer c ON pc.c_id = c.id " +
            "WHERE c.customer_code = #{customerName}")
    List<Map> selectProductsByCustomerName(@Param("customerName") String customerName);

}
